//
//  WbWelcomeController.swift
//  Weibo
//
//  Created by HH on 16/12/4.
//  Copyright © 2016年 Weibo. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WbWelcomeController: UIViewController {
    
    lazy private var backImageView:UIImageView = UIImageView(image: #imageLiteral(resourceName: "ad_background"))
    
    lazy private var iconView:UIImageView = {
        
        let imageView = UIImageView()
        //avatar_default_big
        if let imageUrl = WbUserAccountViewModel.sharedUserAccount.userInfo?.avatar_large{
            imageView.sd_setImage(with: URL(string:imageUrl), placeholderImage: #imageLiteral(resourceName: "avatar_default_big"))
            
        }
        
        imageView.sizeToFit()
        
        imageView.layer.cornerRadius = 42.5
        imageView.clipsToBounds = true
        
        return imageView
        
    }()
    
    lazy private var messageLabel:UILabel! = {
        
        let label = UILabel()
        
        if let name = WbUserAccountViewModel.sharedUserAccount.userInfo?.name{
            
            label.text = "欢迎回来,\(name)"
            
        }else{
            label.text = "欢迎回来"
        }
        
        label.font = UIFont.systemFont(ofSize: 13)
        
        label.alpha = 0
        
        return label
        
    }()
    
    
    
    private func startAnimation(){
        
        self.iconView.snp.updateConstraints({ (make) in
            make.top.equalToSuperview().offset(100)
        })
        
        UIView.animate(withDuration: 1.0, delay: 0.5, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {() -> Void in
            
            //要想看见动画,要调这个方法
            self.view.layoutIfNeeded()
            
        
        
        }, completion: {(_) -> Void in
        
            
            UIView.animate(withDuration: 1.0, animations: {
                
                self.messageLabel.alpha = 1
                
            }, completion: { (_) in
                
                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+1.5) {
                    print("切换到登录后的主视图控制器")
                    
                    NotificationCenter.default.post(name: NSNotification.Name(SwitchViewController), object: self)
                }
                
            })
        })
    }
    
    
    
    override func loadView() {
        view = backImageView
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
        
        // Do any additional setup after loading the view.
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    
    func setupUI(){
        
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        iconView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(300)
             make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        messageLabel.snp.makeConstraints { (make) in
            make.top.equalTo(iconView.snp.bottom).offset(marign)
            make.centerX.equalTo(iconView)
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
